package com.practice.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.sql.SQLException;

/**
 * <p>
 * 快速生成
 * </p>
 *
 * @author lanjerry
 * @since 2021-09-16
 */
public class FastAutoGeneratorTest {

    /**
     * 执行初始化数据库脚本
     */
//    public static void before() throws SQLException {
//        Connection conn = DATA_SOURCE_CONFIG.build().getConn();
//        InputStream inputStream = H2CodeGeneratorTest.class.getResourceAsStream("/sql/init.sql");
//        ScriptRunner scriptRunner = new ScriptRunner(conn);
//        scriptRunner.setAutoCommit(true);
//        scriptRunner.runScript(new InputStreamReader(inputStream));
//        conn.close();
//    }

    /**
     * 数据源配置
     */
    private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig
            .Builder("jdbc:mysql:///practice?setUnicode=true&characterEncoding=utf8", "sa", "yangjie");

    /**
     * 执行 run
     */
    public static void main(String[] args) throws SQLException {
        String basePath = System.getProperty("user.dir");
        String sTemplatePath = "/templates/myService.java.vm";//service模板路径
        String cTemplatePath = "/templates/controller.java.vm";//controller模板路径

        FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                // 全局配置
                .globalConfig(builder -> builder.author("xieqc").outputDir(basePath + "/src/main/java").enableSwagger())
                // 包配置
                .packageConfig(builder -> builder.parent("com.practice").serviceImpl("service"))
                // 策略配置
                .strategyConfig(builder -> builder.addInclude("s_user")
                        .addTablePrefix("s_")
                        .serviceBuilder()//开启服务构建
                        .formatServiceImplFileName("%sService")
                        .entityBuilder()//开启entity构建
                        .enableLombok()
                        .controllerBuilder()//
                        .enableRestStyle()
                )

                .templateEngine(new VelocityTemplateEngine())
                .templateConfig(builder -> builder.service(null).serviceImpl(sTemplatePath).controller(cTemplatePath))
                /*
                    模板引擎配置，默认 Velocity 可选模板引擎 Beetl 或 Freemarker
                   .templateEngine(new BeetlTemplateEngine())
                   .templateEngine(new FreemarkerTemplateEngine())
                 */
                .execute();
    }
}
